package com.zhuxiang.test4;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * 客户端
 */
public class Client1 {

    public static void main(String[] args) {
        Socket s = null;
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            s = new Socket("127.0.0.1",8888);
            System.out.println("连接服务器成功");
            oos = new ObjectOutputStream(s.getOutputStream());
            UserMessage tum = new UserMessage("check",new User("admin","12345"));
            oos.writeObject(tum);
            oos.flush();
            ois = new ObjectInputStream(s.getInputStream());
            UserMessage msg = (UserMessage) ois.readObject();
            System.out.println(msg);
            if ("success".equals(msg.getType())){
                System.out.println("登录成功");
            }else {
                System.out.println("登录失败");
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != oos){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != s){
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
